import { HttpClient } from '@angular/common/http'
import { Component, OnInit, ViewChild } from '@angular/core'
import { MatPaginator } from '@angular/material'
import { Observable, of } from 'rxjs'
import { catchError, debounceTime, map, startWith, switchMap, merge } from 'rxjs/operators'
import AppApi from '../../../api/index'
import { AppDrawerComponent } from '../../../common/drawer/app-drawer.component'
import { buildPageBuilder, jsonToPage, Page } from '../../../common/pageUtil'
import { Publication } from '../publication'
import { pipe } from '@angular/core/src/render3/pipe'

@Component({
  selector: 'PublicationsPage',
  templateUrl: './pubs-page.component.html',
  styleUrls: ['./pubs-page.component.less']
})
export class PubsPageComponent implements OnInit {

  @ViewChild(MatPaginator) paginator: MatPaginator
  @ViewChild('appDrawer')
  private appDrawer: AppDrawerComponent
  private _pubList$: Observable<any[]>
  private _pageParams = new Page<any>()

  pubDetail: Publication = new Publication()
  pageRequest = buildPageBuilder()
  pubLoading = false

  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    this.pubList$ = this.paginator.page.pipe(
      startWith({}),
      debounceTime(300),
      switchMap((pageEvent) => {
        this.pubLoading = true
        return this.http.post<Page<any>>(
          AppApi.PubApi.allPage,
          this.pageRequest
            .buildPageNum(this.paginator.pageIndex + 1)
            .buildPageSize(this.paginator.pageSize)
            .concatData({ keyword: '', pubType: '' })
        )
      }),
      map((page => {
        this.pubLoading = false
        this.pageParams = jsonToPage(page)
        return page.content
      })),
      catchError(() => {
        this.pubLoading = false
        return of([])
      })
    )
  }

  /**
   * 刷新公示列表
   */
  refreshPubPage() {
    this.pubLoading = true
    this.pubList$ = this.http.post<Page<any>>(
      AppApi.PubApi.allPage,
      this.pageRequest
        .buildPageNum(this.paginator.pageIndex + 1)
        .buildPageSize(this.paginator.pageSize)
        .concatData({ keyword: '', pubType: '' })
    ).pipe(
      map(page => {
        this.pubLoading = false
        this.pageParams = jsonToPage(page)
        return page.content
      })
    )
  }

  /**
   *  抽取公示信息
   * @param pub
   */
  getPubListText(pub: any): string {
    let text = ''
    if (pub.type) text = text.concat(`[${this.showPubTypeCN(pub.type)}]`)
    if (pub.title) text = text.concat(`${pub.title}`)
    return text
  }
  showDetail(pubInfo: any): void {
    this.http.post<Publication>(
      AppApi.PubApi.pubDetail,
      { pubId: pubInfo.id }
    ).pipe().subscribe(pub => {
      this.pubDetail = pub
      this.appDrawer.onToggleDrawer(true)
    })
    // this.pubDetail.content = this.drawerText
  }
  closeDetail(): void {
    this.appDrawer.onToggleDrawer(false)
  }

  showPubTypeCN(pubType: String) {
    switch (pubType) {
      case 'InformationPublication': return '消息'
      case 'ActivityPublication': return '活动'
      default: return '消息'
    }
  }

  public get pubList$(): Observable<any[]> {
    return this._pubList$
  }
  public set pubList$(value: Observable<any[]>) {
    this._pubList$ = value
  }

  public get pageParams() {
    return this._pageParams
  }
  public set pageParams(value) {
    this._pageParams = value
  }
}
